#ifndef TARGET_H
#define TARGET_H

#include <map>
#include <array>
#include <utility>
#include <opencv2/opencv.hpp>

#include "video_encode.h"
#include "dvpp_process.h"
#include "utils.h"
#include "thread_safe_queue.h"

// 自己定制要保存的数据类型
typedef struct StorageData {
    cv::Mat bgr;
    int image_id;
} StorageData;


class TargetStorage {
public:
    explicit TargetStorage();

    ~TargetStorage();

    void DestroyResource();

    void init(uint32_t width, uint32_t height, std::string outputPath, aclrtContext& context);

    Result StorageDataEnQueue(cv::Mat& bgr, int image_id);

    void save_as_txt(const vector<BBox> &dets, string fileName, uint32_t origin_width, uint32_t origin_height, bool isSaveConf);

    void cv_write_image(cv::Mat& img, uint32_t frame);

    void dvpp_write_image(const cv::Mat& img, uint32_t frame);
    void dvpp_encode_image(const cv::Mat& src, ImageData& dst);

    void cv_write_video(cv::Mat& img);

    void dvpp_write_video(cv::Mat& img);

private:
    static void TargetStorageThreadFunction(void* senderSelf); 


    AtlasError SetAclContext();

    cv::VideoWriter writer;
    VideoEncode encoder_;
    aclrtContext context_;
    aclrtStream stream_;
    DvppProcess dvpp_;
    

    ThreadSafeQueue<StorageData> StorageDataQueue_;
    bool inited_;

    std::string outputPath_;
};

#endif //TARGET_H
